package com.tonysong.oa.yeboa12.config.security;

import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //配置密码加密
    @Bean
    public PasswordEncoder passwordEncoder() {
        return  new BCryptPasswordEncoder();
    }
    //配置认证密码的UserDetaisService接口的实现
   @Resource
   private UserDetailsService loginService;

    @Resource
    private JwtFilterToken jwtFilterToken;

    @Resource
    private RestAuthorizationEntryPoint restAuthorizationEntryPoint;
    @Resource
    private RestfulAccessDeniedHandler restfulAccessDeniedHandler;


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //配置认证对象：
        auth.userDetailsService(this.loginService)
                .passwordEncoder(this.passwordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //配置忽略拦截请求的url
        web.ignoring().antMatchers(
        "/admin/login",
                "/logout",
                "/hello",
                "/css/**",
                "/js/**",
                "favicon.ico",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources/**",
                "/v2/api-docs/**",
                "/captcha",
                "/ws/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置拦截规则
        http
                .csrf()
                .disable()
                .sessionManagement()
                //禁用session
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                //头部请求使用缓存处理
                .headers()
                .cacheControl();
        //将jwt的过滤器注册到密码验证的过滤器的前面！
         http.addFilterBefore(this.jwtFilterToken,UsernamePasswordAuthenticationFilter.class);
        //设置异常处理的过滤器（以json形式输出）
        http.exceptionHandling()
                .accessDeniedHandler(this.restfulAccessDeniedHandler)
                .authenticationEntryPoint(this.restAuthorizationEntryPoint);
    }
}
